<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php

$file_pic = "";

echo $_POST["publish_content"] . "<br/>";

if ($_FILES["publish_picture"]["error"] > 0) {
  echo "Return Code: " . $_FILES["publish_picture"]["error"] . "<br/>";
} else {
  echo "Upload: " . $_FILES["publish_picture"]["name"] . "<br />";
  echo "Type: " . $_FILES["publish_picture"]["type"] . "<br />";
  echo "Size: " . ($_FILES["publish_picture"]["size"] / 1024) . " Kb<br />";
  echo "Temp file: " . $_FILES["publish_picture"]["tmp_name"] . "<br />";

  echo "Picture type : " . $_FILES["publish_picture"]["type"] . "<br />";

  $random_name = "default.jpg";
  if ($_FILES["publish_picture"]["type"] == "image/gif") {
    $random_name = time() . ".gif";
  } else if ($_FILES["publish_picture"]["type"] == "image/jpeg") {
    $random_name = time() . ".jpg";
  } else if ($_FILES["publish_picture"]["type"] == "image/pjpeg") {
    $random_name = time() . ".png";
  }


  move_uploaded_file($_FILES["publish_picture"]["tmp_name"], "/var/www/platform/upload/" . $random_name);
  if (file_exists("/var/www/platform/upload/" . $random_name)) {
    echo "file stored ! <br/>";
  } else {
    echo "file store failed! <br/>";
  }
  echo "Stored in: " . "/var/www/platform/upload/" . $random_name;
  $file_pic = "upload/" . $random_name;

}


function GetIP(){
if(!empty($_SERVER["HTTP_CLIENT_IP"])) {
  $cip = $_SERVER["HTTP_CLIENT_IP"];
} else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
  $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if(!empty($_SERVER["REMOTE_ADDR"])){
  $cip = $_SERVER["REMOTE_ADDR"];
}
else{
  $cip = "Unknown User";
}
  return $cip;
}

$uname = GetIP();


$con = mysql_connect("localhost","root","123456");
if (!$con) {
  echo "my sql not connect<br/>";
} else if ($_POST["publish_content"] != "") {
  echo "mysql connected!<br/>";

  mysql_select_db("platform", $con);
  mysql_query("insert into publish values(NULL, '" . $uname . "', '" . $_POST["publish_content"] . "', '" . $file_pic . "')");

  mysql_close($con);
}

echo "<script>location.href='http://10.120.137.109/platform/index.php';</script>";


?>